package com.rxnetwork.sample.mvp.toolbar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.rxnetwork.sample.R;
import com.rxnetwork.sample.utils.StatusBarUtil;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.lang.reflect.Field;

/**
 * by xiaocode
 * 带标题栏的fragment类
 */
public abstract class BaseTopBarFragment extends RxFragment {

    private TopbarLayout mTopBarLayout;

    private FrameLayout mContentLayout;

    private View getContentViewLayout() {
        View view = View.inflate(getActivity(), R.layout.activity_top_bar, null);
        mTopBarLayout = (TopbarLayout) view.findViewById(R.id.top_bar_layout);
        mTopBarLayout.setTitle(getTopTitle());
        try {
            //处理android 4.4以下会崩溃
            StatusBarUtil.setColor(getActivity(), mTopBarLayout.getTopbarBgColor() > 0 ? mTopBarLayout.getTopbarBgColor() :
                    getResources().getColor(android.R.color.white), 0);
        } catch (Exception e) {
        }
        mContentLayout = (FrameLayout) view.findViewById(R.id.content_layout);
        if (getContentViewResId() > 0) {
            View.inflate(getActivity(), getContentViewResId(), mContentLayout);
        }
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContentViewResId() > 0) {
            getActivity().setContentView(getContentViewLayout());
        }
        try {
            setTopBarLayoutColor(getResources().getColor(android.R.color.white));
        } catch (Exception e) {
        }
        initPresenter();
        initViewsAndEvents();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (getContentViewResId() > 0) {
            view = inflater.inflate(getContentViewResId(), container, false);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViewsAndEvents();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        // for bug ---> java.lang.IllegalStateException: Activity has been destroyed
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public void setTopBarLayoutColor(int color) {
        mTopBarLayout.setTopbarBgColor(color);
        StatusBarUtil.setColor(getActivity(), color, 0);
    }

    public TopbarLayout getTopBarLayout() {
        return mTopBarLayout;
    }

    /**
     * 便于设置全局崩溃，网络错误，空白页
     *
     * @param contentLayout
     */
    public void setContentLayout(FrameLayout contentLayout) {
        this.mContentLayout = contentLayout;
    }

    public FrameLayout getContentLayout() {
        return mContentLayout;
    }

    protected abstract void initPresenter();

    protected abstract void initViewsAndEvents();

    protected abstract int getContentViewResId();

    protected abstract CharSequence getTopTitle();

}
